<template>
  <div class="user-add-back">
    <el-form :model="addRuleForm" status-icon :rules="rules" ref="addRuleForm" label-width="100px" class="addRuleForm">
      <el-form-item label="姓名" prop="username">
        <el-input type="text" v-model="addRuleForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="工号" prop="userId">
        <el-input type="text" v-model="addRuleForm.userId" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input type="text" v-model="addRuleForm.phone" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="addRuleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="addRuleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('addRuleForm')">提交</el-button>
        <el-button @click="resetForm('addRuleForm')">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-link type="primary" href="../login">返回登录</el-link>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
import axios from "../../plugins/axiosConfig";
import {userAddUrl} from "../../utils/api";

export default {
  data() {
    var checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入姓名'));
      } else {
        callback();
      }

    }
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入手机号'));
      } else if (value.length !== 11) {
        return callback(new Error('手机号长度为11位'));
      } else {
        callback();
      }
    }
    var checkUserId = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入工号'));
      } else if (value.length !== 6) {
        return callback(new Error('工号长度为6位'));
      } else {
        callback();
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.addRuleForm.checkPass !== '') {
          this.$refs.addRuleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.addRuleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      addRuleForm: {
        username: '',
        userId: '',
        phone: '',
        pass: '',
        checkPass: '',
      },
      rules: {
        userId: [
          {validator: checkUserId, trigger: 'blur'}
        ],
        pass: [
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'}
        ],
        username: [
          {validator: checkUsername, trigger: 'blur'}
        ],
        phone: [
          {validator: checkPhone, trigger: 'blur'}
        ]

      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          var data = {
            username: this.$data.addRuleForm.username, userId: this.$data.addRuleForm.userId,
            telephone: this.$data.addRuleForm.phone, password: this.$data.addRuleForm.pass
          }

          axios.post(userAddUrl, data,
              {
                'Content-Type': 'application/json'
              }
          ).then((res) => {
            console.log("--------res", res)
            if (res.status === 200) {
              if (res.data.data === true) {
                alert("注册成功!")
              }
              return res.data
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>
.user-add-back {
  display: block;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
}

.mid-row {
  height: 30px;
  margin-top: 20px;
}

.addRuleForm {
  margin-top: 10%;
  width: 80%;
  margin-right: 10%;
}

</style>